#include "test6.h"

/* 查找 */
void test6(void)
{
    status ret;
    data_type data;
    sqlist L;
    ret = ListInit(&L);
    if( ret != ERROR )
        printf("sqlist init TURE\n ");
    else
        printf("sqlist init ERROR\n");
    ret = Banary_Search(&L);
    if( ret != ERROR )
        printf("Search successful i = %d\n ", ret+1);
    else
        printf("Lookup failed\n");
}

/* 折中查找 */
int Banary_Search(sqlist *list)
{
    ListInsert(list, 1, 8);
    ListInsert(list, 2, 22);
    ListInsert(list, 3, 38);
    ListInsert(list, 4, 41);
    ListInsert(list, 5, 58);
    ListInsert(list, 6, 60);
    ListInsert(list, 7, 79);
    ListInsert(list, 8, 85);
    ListInsert(list, 9, 97);
    ListInsert(list, 9, 100);
    // ListAllprint(list);

    int target = 8;
    int high=0, low=0, mid=0;
    high = MaxNum;
    while(1){
        mid = (high+low)/2.0f;
        if( list->data[mid] == target ){
            return mid;
        }
        else if(low >= high)
            return ERROR;
        else if( list->data[mid] > target ){
            high = mid-1; 
        }
        else if( list->data[mid] < target ){
            low = mid+1;
        }
    }
}
